package com.spit.hkpost.bean

import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import com.spit.hkpost.bean.dao.ConfigDao
import com.spit.hkpost.bean.dao.RfidDao
import com.spit.hkpost.bean.db.ConfigBean
import com.spit.hkpost.bean.db.RfidBean
import com.spit.hkpost.mar.MyApplication


/**
 * @Author nike
 * @Date 2023/7/11 18:00
 * @Description
 */
@Database(entities = [
    ConfigBean::class,
    RfidBean::class,
], version = 1, exportSchema = false)
abstract class AppRoomDataBase : RoomDatabase() {

    abstract fun getConfigDao(): ConfigDao

    abstract fun getRfidDao(): RfidDao

    companion object {

        @Volatile
        private var databaseInstance: AppRoomDataBase? = null

        @Synchronized
        @JvmStatic
        fun get(): AppRoomDataBase {
            if (databaseInstance == null) {
                databaseInstance = Room.databaseBuilder(
                    MyApplication.get(),
                    AppRoomDataBase::class.java,
                    "HKpost.db"
                )
                    .addMigrations(MIGRATION_1_2) // 应用迁移
                    .build()
            }
            return databaseInstance!!
        }

        val MIGRATION_1_2 = object : Migration(1, 2) {
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("ALTER TABLE RfidBean ADD COLUMN today TEXT")
            }
        }
    }

}